package com.ycuwq.datepicker.interpolator;

import ohos.agp.render.Path;
import ohos.agp.utils.Point;

/**
 * Path interpolator
 */
public class PathInterpolator extends BaseInterpolator {
    /**
     * * The constant PRECISION This governs how accurate the approximation of the Path is.
     */
    private static final float PRECISION = 0.002f;

    /**
     * M x
     */
    private float[] mX; // x coordinates in the line

    /**
     * M y
     */
    private float[] mY; // y coordinates in the line

    /**
     * Path interpolator
     *
     * @param path path
     */
    public PathInterpolator(Path path) {
        initPath(path);
    }

    /**
     * Path interpolator
     *
     * @param controlX control x
     * @param controlY control y
     */
    public PathInterpolator(float controlX, float controlY) {
        initQuad(controlX, controlY);
    }

    /**
     * Path interpolator
     *
     * @param controlX1 control x 1
     * @param controlY1 control y 1
     * @param controlX2 control x 2
     * @param controlY2 control y 2
     */
    public PathInterpolator(float controlX1, float controlY1, float controlX2, float controlY2) {
        initCubic(controlX1, controlY1, controlX2, controlY2);
    }


    /**
     * Init quad *
     *
     * @param controlX control x
     * @param controlY control y
     */
    private void initQuad(float controlX, float controlY) {
        Path path = new Path();
        path.moveTo(0, 0);
        path.quadTo(controlX, controlY, 1f, 1f);
        initPath(path);
    }

    /**
     * Init cubic *
     *
     * @param x1 x 1
     * @param y1 y 1
     * @param x2 x 2
     * @param y2 y 2
     */
    private void initCubic(float x1, float y1, float x2, float y2) {
        Path path = new Path();
        path.moveTo(0, 0);
        path.cubicTo(new Point(x1, y1), new Point(x2, y2), new Point(1f, 1f));
        initPath(path);
    }

    /**
     * Init path *
     *
     * @param path path
     */
    private void initPath(Path path) {

    }

    /**
     * Get interpolation float
     *
     * @param input input
     * @return the float
     */
    @Override
    public float getInterpolation(float input) {
        if (input <= 0) {
            return 0;
        } else if (input >= 1) {
            return 1;
        }
        // Do a binary search for the correct x to interpolate between.
        int startIndex = 0;
        int endIndex = mX.length - 1;

        while (endIndex - startIndex > 1) {
            int midIndex = (startIndex + endIndex) / 2;
            if (input < mX[midIndex]) {
                endIndex = midIndex;
            } else {
                startIndex = midIndex;
            }
        }

        float xRange = mX[endIndex] - mX[startIndex];
        if (xRange == 0) {
            return mY[startIndex];
        }

        float tInRange = input - mX[startIndex];
        float fraction = tInRange / xRange;

        float startY = mY[startIndex];
        float endY = mY[endIndex];
        return startY + (fraction * (endY - startY));
    }
}
